package com.jkl.controller.notice;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jkl.dao.notice.PnNoticeMapper;
import com.jkl.entity.BaseResponseDTO;
import com.jkl.entity.notice.PnNotice;
import com.jkl.entity.po.user.BSUser;
import com.jkl.entity.project.po.PJProjectPicture;
import com.jkl.service.notice.inter.PnNoticeServiceInterface;
import com.jkl.untils.DownloadFile;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * <p>
 * 123开放层接口
 * <p>
 * <p>
 * 变更说明：wangjingxiang 2017年12月28日 123 增加新增、更新、删除方法
 * <p>
 * Date:2018年11月18日
 * 
 * @author ji.k
 * @version 1.0
 */
@RestController
@RequestMapping("/jky/notice")
@Slf4j
public class PnNoticeControllerV1 {

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private PnNoticeServiceInterface pnNoticeServiceInterface;
	@Value("${systemconfig.file.url}")
	private String picUrl;
	/**
	 * 新增
	 *     
	 * @param request
	 * @param response
	 * @return
	 */     
	@PostMapping("/addNotice")  
	public BaseResponseDTO addProject(HttpServletRequest request, HttpServletResponse response) {
		String data = request.getParameter("form");
		PnNotice noticeInfo = JSON.parseObject(data, PnNotice.class);
		noticeInfo.setState("1");
		List<MultipartFile> fileList = null;
		// 将当前上下文初始化给 CommonsMutipartResolver （多部分解析器）
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
				request.getSession().getServletContext());
		if (multipartResolver.isMultipart(request)) {    
			// 将request变成多部分request
			MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
			fileList = multiRequest.getFiles("files");
		}   
		BSUser user = (BSUser) request.getSession().getAttribute("user");
		noticeInfo.setOperId(String.valueOf(user.getPersonId()))
				.setOperTime(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()));
		pnNoticeServiceInterface.addNotice(noticeInfo, fileList);

		return new BaseResponseDTO();
	}

//	@PostMapping("/noticeList")
//	public BaseResponseDTO noticeList(HttpServletRequest request, HttpServletResponse response,
//			@RequestBody PnNotice PnNotice) {
//		BaseResponseDTO dto = new BaseResponseDTO();
//		List<PnNotice> pnNoticeList = pnNoticeServiceInterface.queryPnNoticeList(PnNotice);
//		JSONObject returnJson = new JSONObject();
//		JSONArray paramArray = (JSONArray) JSONArray.toJSON(pnNoticeList);
//		returnJson.put("noticeList", paramArray);
//		int count = pnNoticeServiceInterface.queryCount(PnNotice);
//		returnJson.put("count", count);
//		dto.setReturnData(returnJson);
//		return dto;
//	}

	@PostMapping("/noticeList")  
	public BaseResponseDTO noticeList(@RequestBody PnNotice param) {
		BaseResponseDTO returnData = new BaseResponseDTO();
		PageInfo<PnNotice> projectList = pnNoticeServiceInterface.queryPnNoticeList(param);
		JSONObject returnJson = new JSONObject();
		returnJson.put("projectList", projectList);
		returnData.setReturnData(returnJson);
		return returnData;    
	}
  
	@PostMapping("/noticeObject") // 查询通知详情

	public PnNotice noticeObject(@RequestParam(value = "rowId") String rowId) {
		return pnNoticeServiceInterface.noticeObject(rowId);
	}

	@PostMapping("/updateNotice") // 更新
	public int updateNotice(@RequestBody PnNotice param) {
		return pnNoticeServiceInterface.updateNotice(param);
	}

	@PostMapping("/deleteNotice") // 删除
	public int deleteNotice(@RequestParam(value = "rowId") String rowId) {
		return pnNoticeServiceInterface.deleteNotice(rowId);
	}

	@PostMapping("/selectFive") // 查询前五
	public List<PnNotice> selectFive(@RequestParam(value = "type") String type) {
		return pnNoticeServiceInterface.selectFive(type);
	}

	@GetMapping("/noticeDetail")
	public BaseResponseDTO noticeDetail(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "rowId") String rowId) {
		BaseResponseDTO dto = new BaseResponseDTO();
		PnNotice notice = pnNoticeServiceInterface.queryPnNoticeDetail(rowId);
		JSONObject returnJson = (JSONObject) JSON.toJSON(notice);
		dto.setReturnData(returnJson);
		return dto;
	}
	  
	@PostMapping("/addNoticeList")  
	public void addNoticeList(@RequestBody PnNotice[] param) {//System.out.print(param[3]);
	
	for(int i=0;i<param.length;i++) {
		PnNotice notice=param[i];
		PnNotice noticeOne = pnNoticeServiceInterface.queryNotice(notice.getRowId());
		if(noticeOne==null) {
			pnNoticeServiceInterface.insertNotice(notice);
		}else {
			if(!notice.getOperTime().equals(noticeOne.getOperTime())){
			pnNoticeServiceInterface.updateNoticeBySuper(notice);
			}
		}
	}
  }	
		     
	@PostMapping("/addProjectPicture")  
	public void addProjectPicture(@RequestBody PJProjectPicture[] param) {
	
	  for(int i=0;i<param.length;i++) {
		PJProjectPicture projectPicture=param[i];
		PJProjectPicture projectPictureOne = pnNoticeServiceInterface.queryProjectPicture(projectPicture.getRowId());
		if(projectPictureOne==null) {
			DownloadFile.downloadFile(projectPicture.getPicUrl());
			String pickName=projectPicture.getPicUrl().substring(projectPicture.getPicUrl().lastIndexOf("/"));
			projectPicture.setPicUrl(picUrl+pickName);
			pnNoticeServiceInterface.insertProjectPicture(projectPicture);
		}else {
			if(!projectPicture.getStatus().equals(projectPictureOne.getStatus())){  
			pnNoticeServiceInterface.updateProjectPicture(projectPicture);
			}
		}  
	}
		System.out.println("================"+picUrl);
		    
	
	}
}